package com.hanbang.taitaigou.ui.activity.aboutme.G20;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hanbang.taitaigou.R;
import com.hanbang.taitaigou.ui.activity.base.BaseActivity;
import com.hanbang.taitaigou.utils.http.HttpCallBack;
import com.hanbang.taitaigou.utils.http.HttpRequestParams;
import com.hanbang.taitaigou.utils.ui.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by fanxin on 2016/3/21.
 */
public class AboutG20ShoppingCardActivity extends BaseActivity {
    //钱
    private TextView tv_50, tv_100, tv_200, tv_500, tv_1000;
    //选择支付宝或微信
    private LinearLayout ll_zfb, ll_wx;
    private CheckBox cb_zfb, cb_wx;

    private int money = 50;

    private Button button;

    private TextView tv_tishi;

    private LinearLayout ll_czje;
     //充值
    private LinearLayout ll_chongzhi;
    private TextView tv_chongzhi;

    private LinearLayout ll_user_name;
    private LinearLayout ll_password;
    //提现
    private LinearLayout ll_tixian;
    private TextView tv_tixian;

    //G20卡号密码
    private EditText et_username;
    private EditText et_password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about_g20_shopping_card);
        initView();
        setOnClickListener();
        RechargeOrWithdrawals(1);
    }

    /*
     * 设置点击事件
     */
    private void setOnClickListener() {
        tv_50.setOnClickListener(onClickListener);
        tv_100.setOnClickListener(onClickListener);
        tv_200.setOnClickListener(onClickListener);
        tv_500.setOnClickListener(onClickListener);
        tv_1000.setOnClickListener(onClickListener);

        ll_zfb.setOnClickListener(onClickListener);
        ll_wx.setOnClickListener(onClickListener);

        ll_chongzhi.setOnClickListener(onClickListener);
        ll_tixian.setOnClickListener(onClickListener);
        button.setOnClickListener(onClickListener);
    }

    @SuppressWarnings("unused")
    private View.OnClickListener onClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                //选择钱
                case R.id.tv_50:
                    selectMoney(50);
                    break;
                case R.id.tv_100:
                    selectMoney(100);
                    break;
                case R.id.tv_200:
                    selectMoney(200);
                    break;
                case R.id.tv_500:
                    selectMoney(500);
                    break;
                case R.id.tv_1000:
                    selectMoney(1000);
                    break;
                //选择支付宝或微信
                case R.id.ll_zfb:
                    cb_zfb.setBackgroundResource(R.mipmap.check_box_full);
                    cb_zfb.setChecked(true);
                    cb_wx.setBackgroundResource(R.mipmap.check_box_null);
                    cb_wx.setChecked(false);
                    break;
                case R.id.ll_wx:
                    cb_zfb.setBackgroundResource(R.mipmap.check_box_null);
                    cb_zfb.setChecked(false);
                    cb_wx.setBackgroundResource(R.mipmap.check_box_full);
                    cb_wx.setChecked(true);
                    break;
                //充值
                case R.id.ll_chongzhi:
                    RechargeOrWithdrawals(1);
                    break;
                //提现
                case R.id.ll_tixian:
                    RechargeOrWithdrawals(2);
                    break;
                //充值或提现按钮
                case R.id.button:
//                    validateG20();
                    break;
                default:
                    break;
            }
        }
    };

    private void validateG20(){
        HttpRequestParams httpRequestParams = new HttpRequestParams("ValidateG20");
        httpRequestParams.addUserId(userData);
        httpRequestParams.addBodyParameter("Card_no", et_username.getText().toString());
        httpRequestParams.addBodyParameter("Card_pwd",et_password.getText().toString());
        httpManager.post(httpRequestParams, new HttpCallBack<String>(this, "正在充值请稍等...") {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if(jsonObject.getInt("status") == 0){
                        ToastUtils.show(AboutG20ShoppingCardActivity.this,jsonObject.getString("msg"), Toast.LENGTH_LONG);
                    }else{
                        ToastUtils.show(AboutG20ShoppingCardActivity.this,jsonObject.getString("msg"), Toast.LENGTH_LONG);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    private void RechargeOrWithdrawals(int i){
        if(i == 1){
            ll_chongzhi.setBackgroundResource(R.drawable.aboutme_money_2_background);
            ll_tixian.setBackgroundResource(R.drawable.aboutme_money_background);
            ll_zfb.setVisibility(View.GONE);
            ll_wx.setVisibility(View.GONE);
            button.setText(getResources().getString(R.string.bottom_recharge));
            tv_tishi.setVisibility(View.GONE);
            ll_czje.setVisibility(View.GONE);
            ll_user_name.setVisibility(View.VISIBLE);
            ll_password.setVisibility(View.VISIBLE);

        }else if (i == 2){
            ll_chongzhi.setBackgroundResource(R.drawable.aboutme_money_background);
            ll_tixian.setBackgroundResource(R.drawable.aboutme_money_2_background);
            ll_zfb.setVisibility(View.VISIBLE);
            ll_wx.setVisibility(View.VISIBLE);
            button.setText(getResources().getString(R.string.bottom_withdrawals));
            tv_tishi.setVisibility(View.VISIBLE);
            ll_czje.setVisibility(View.VISIBLE);
            ll_user_name.setVisibility(View.GONE);
            ll_password.setVisibility(View.GONE);
        }
    }


    /**
     * 选择要充值或提现的金额
     */
    private void selectMoney(int a){
        if(a ==50){
            tv_50.setBackgroundResource(R.drawable.aboutme_money_2_background);
            tv_100.setBackgroundResource(R.drawable.aboutme_money_background);
            tv_200.setBackgroundResource(R.drawable.aboutme_money_background);
            tv_500.setBackgroundResource(R.drawable.aboutme_money_background);
            tv_1000.setBackgroundResource(R.drawable.aboutme_money_background);
            money = Integer.parseInt(tv_50.getText().toString());
        }else if(a == 100){
            tv_50.setBackgroundResource(R.drawable.aboutme_money_background);
            tv_100.setBackgroundResource(R.drawable.aboutme_money_2_background);
            tv_200.setBackgroundResource(R.drawable.aboutme_money_background);
            tv_500.setBackgroundResource(R.drawable.aboutme_money_background);
            tv_1000.setBackgroundResource(R.drawable.aboutme_money_background);
            money = Integer.parseInt(tv_100.getText().toString());
        }else if(a == 200){
            tv_50.setBackgroundResource(R.drawable.aboutme_money_background);
            tv_100.setBackgroundResource(R.drawable.aboutme_money_background);
            tv_200.setBackgroundResource(R.drawable.aboutme_money_2_background);
            tv_500.setBackgroundResource(R.drawable.aboutme_money_background);
            tv_1000.setBackgroundResource(R.drawable.aboutme_money_background);
            money = Integer.parseInt(tv_200.getText().toString());
        } else if(a == 500){
            tv_50.setBackgroundResource(R.drawable.aboutme_money_background);
            tv_100.setBackgroundResource(R.drawable.aboutme_money_background);
            tv_200.setBackgroundResource(R.drawable.aboutme_money_background);
            tv_500.setBackgroundResource(R.drawable.aboutme_money_2_background);
            tv_1000.setBackgroundResource(R.drawable.aboutme_money_background);
            money = Integer.parseInt(tv_500.getText().toString());
        }else if(a ==1000){
            tv_50.setBackgroundResource(R.drawable.aboutme_money_background);
            tv_100.setBackgroundResource(R.drawable.aboutme_money_background);
            tv_200.setBackgroundResource(R.drawable.aboutme_money_background);
            tv_500.setBackgroundResource(R.drawable.aboutme_money_background);
            tv_1000.setBackgroundResource(R.drawable.aboutme_money_2_background);
            money = Integer.parseInt(tv_1000.getText().toString());
        }
    }


    /*
     * 初始化view
     */
    private void initView() {
        // 设置顶部的标题
        setTop(null, 0, getString(R.string.top_g20_shopping_card), null, 0);
        setBackArrows(findViewById(R.id.top_bar_left_iv));

        tv_50 = (TextView) findViewById(R.id.tv_50);
        tv_100 = (TextView) findViewById(R.id.tv_100);
        tv_200 = (TextView) findViewById(R.id.tv_200);
        tv_500 = (TextView) findViewById(R.id.tv_500);
        tv_1000 = (TextView) findViewById(R.id.tv_1000);

        ll_zfb = (LinearLayout) findViewById(R.id.ll_zfb);
        ll_wx = (LinearLayout) findViewById(R.id.ll_wx);

        cb_zfb = (CheckBox) findViewById(R.id.cb_zfb);
        cb_wx = (CheckBox) findViewById(R.id.cb_wx);

        ll_chongzhi = (LinearLayout)findViewById(R.id.ll_chongzhi);
        tv_chongzhi = (TextView)findViewById(R.id.tv_chongzhi);

        ll_tixian = (LinearLayout)findViewById(R.id.ll_tixian);
        tv_tixian = (TextView)findViewById(R.id.tv_tixian);
        button = (Button)findViewById(R.id.button);
        tv_tishi = (TextView)findViewById(R.id.tv_tishi);
        ll_czje = (LinearLayout)findViewById(R.id.ll_czje);
        ll_user_name = (LinearLayout)findViewById(R.id.ll_user_name);
        ll_password = (LinearLayout)findViewById(R.id.ll_password);
        et_username = (EditText)findViewById(R.id.et_username);
        et_password = (EditText)findViewById(R.id.et_password);
    }
}
